package com.lcq.study.normal;

import java.net.*;
import java.util.Enumeration;

/**
 * @ClassName NetTest
 * @Description
 * @Author lichuanqi
 * @Date 2023/9/21 14:25
 * @Version 1.0
 **/
public class NetTest {
    public static void main(String[] args) throws UnknownHostException, SocketException {
//        String ipAddress = InetAddress.getLocalHost().getHostAddress();
//        System.out.println(ipAddress);

//        NetworkInterface ni = NetworkInterface.getByName("eth0");
//        System.out.println(ni.getDisplayName());
//        Enumeration<InetAddress> inetAddresses = ni.getInetAddresses();
//        while (inetAddresses.hasMoreElements()){
//            InetAddress inetAddress = inetAddresses.nextElement();
//            System.out.println(inetAddress.getHostAddress());
//        }
//
//
//        NetworkInterface wlan = NetworkInterface.getByName("wlan0");
//        System.out.println(wlan.getDisplayName());
//        System.out.println(wlan.getInetAddresses());
//        Enumeration<InetAddress> inetAddresses1 = wlan.getInetAddresses();
//        while (inetAddresses1.hasMoreElements()){
//            InetAddress inetAddress = inetAddresses1.nextElement();
//            System.out.println(inetAddress.getHostAddress());
//        }

//        Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
//        while (networkInterfaces.hasMoreElements()){
//            NetworkInterface networkInterface = networkInterfaces.nextElement();
//            int index = networkInterface.getIndex();
//
//            System.out.println(index+":"+ networkInterface.getDisplayName());
//        }


        Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
        while(networkInterfaces.hasMoreElements()){
            NetworkInterface networkInterface = networkInterfaces.nextElement();
            System.out.println(networkInterface.getName());
            System.out.println(networkInterface.getDisplayName());

            Enumeration<InetAddress> inetAddresses = networkInterface.getInetAddresses();
            while (inetAddresses.hasMoreElements()){
                InetAddress inetAddress = inetAddresses.nextElement();
                if (inetAddress instanceof Inet4Address){
                    System.out.println("ipv4");
                }else if (inetAddress instanceof Inet6Address){
                    System.out.println("ipv6");
                }
                System.out.println(inetAddress.getHostAddress());
                System.out.println("+++++++++++++++++++++++++++++++++++++++++++++");
            }
            System.out.println("----------------------------------------------");
        }
    }

    String getIP() throws SocketException, UnknownHostException {
        String result = getIpEth0();
        if (!result.equals("")){
            return result;
        }
        return InetAddress.getLocalHost().getHostAddress();
    }

    private String getIpEth0() throws SocketException {
        NetworkInterface ethNi = NetworkInterface.getByName("eth0");
        Enumeration<InetAddress> inetAddresses = ethNi.getInetAddresses();
        while (inetAddresses.hasMoreElements()){
            InetAddress inetAddress = inetAddresses.nextElement();
            if (inetAddress instanceof Inet4Address){
                return inetAddress.getHostAddress();
            }
        }
        return "";
    }
}
